package com.chuji.crmcj.adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.act.ClientDetailAct;
import com.chuji.crmcj.bean.InsUseHisInfo;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;

/**
 * Created by w.pitt on 2016/2/3.
 */
public class InsureHistoryAdapter extends BaseExpandableListAdapter {

    private TextView mTvClick;
    private RelativeLayout mRlInsure;
    private ArrayList<String> mData;
    private ExpandableListView mLvHistory;
    private LinearLayout mClick;
    private ImageView mTvArr;
    private LinearLayout ll_next1;
    private LinearLayout ll_next2;
    private TextView mTvInsureData;
    private TextView tv_car_cost;
    private TextView tv_car_loss_insurance;
    private TextView tv_insurance_money;
    private TextView tv_personnel_insurance;
    private TextView tv_compulsory_insurance;
    private TextView tv_third_party;
    private TextView tv_theft_insurance;
    private TextView tv_autoignition_insurance;
    private TextView tv_responsibility_insurance;
    private TextView tv_wading_insurance;
    private TextView tv_scratch_insurance;
    private TextView tv_glass_fracture;
    private TextView tv_not_calculation;
    private ChildViewHolder childViewHolder;
    private InsUseHisInfo insUseHisInfo;
    private TextView mTvName;
    private TextView mTvTel;
    private GroupViewHolder groupViewHolder;

    public InsureHistoryAdapter(Runnable historyUseAct, InsUseHisInfo insUseHisInfo, ExpandableListView mLvHistory) {
        this.insUseHisInfo = insUseHisInfo;
        this.mLvHistory = mLvHistory;
    }

    @Override
    public int getGroupCount() {
        return 5;
//        return insUseHisInfo.getCount() == 0 ? 0 : insUseHisInfo.getCount();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
        int totalGroup = getGroupCount();
        for (int i = 0; i < totalGroup; i++) {
            if (i != groupPosition) {
                mLvHistory.collapseGroup(i);
                groupViewHolder.mTvArr.setImageDrawable(UIUtils.getDrawable(R.drawable.icondown));
                this.notifyDataSetChanged();
            }
        }
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.item_group_history, null);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.mTvArr = (ImageView) convertView.findViewById(R.id.iv_arr);
            groupViewHolder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            groupViewHolder.mTvTel = (TextView) convertView.findViewById(R.id.tv_number);
            groupViewHolder.mTvUseCount = (TextView) convertView.findViewById(R.id.tv_use_count);
//            LinearLayout mll_check_detail = (LinearLayout) convertView.findViewById(R.id.ll_check_detail);
            groupViewHolder.ll_next1 = (LinearLayout) convertView.findViewById(R.id.ll_next1);
            groupViewHolder.ll_next2 = (LinearLayout) convertView.findViewById(R.id.ll_next2);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.mTvName.setText(insUseHisInfo.getApiParamObj().get(groupPosition).getName());
        groupViewHolder.mTvTel.setText(insUseHisInfo.getApiParamObj().get(groupPosition).getTel());
        groupViewHolder.mTvUseCount.setText(insUseHisInfo.getApiParamObj().get(groupPosition).getTotalCount());

        if (!isExpanded) {
            groupViewHolder.mTvArr.setImageDrawable(UIUtils.getDrawable(R.drawable.icondown));
        } else {
            groupViewHolder.mTvArr.setImageDrawable(UIUtils.getDrawable(R.drawable.iconup));
        }

        //跳转到详情界面
        groupViewHolder.ll_next1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(new Intent(UIUtils.getContext(), ClientDetailAct.class));
            }
        });
        groupViewHolder.ll_next2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(new Intent(UIUtils.getContext(), ClientDetailAct.class));
            }
        });
        return convertView;
    }

    static class GroupViewHolder {
        private ImageView mTvArr;
        private TextView mTvName;
        private TextView mTvTel;
        private TextView mTvUseCount;
        private LinearLayout ll_next1;
        private LinearLayout ll_next2;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.layout_insurance, null);
            childViewHolder = new ChildViewHolder();
            childViewHolder.mTvInsureData = (TextView) convertView.findViewById(R.id.tv_insurance_date);
            childViewHolder.tv_car_cost = (TextView) convertView.findViewById(R.id.tv_car_cost);
            childViewHolder.tv_car_loss_insurance = (TextView) convertView.findViewById(R.id.tv_car_loss_insurance);
            childViewHolder.tv_insurance_money = (TextView) convertView.findViewById(R.id.tv_insurance_money);
            childViewHolder.tv_personnel_insurance = (TextView) convertView.findViewById(R.id.tv_personnel_insurance);
            childViewHolder.tv_compulsory_insurance = (TextView) convertView.findViewById(R.id.tv_compulsory_insurance);
            childViewHolder.tv_third_party = (TextView) convertView.findViewById(R.id.tv_third_party);
            childViewHolder.tv_theft_insurance = (TextView) convertView.findViewById(R.id.tv_theft_insurance);
            childViewHolder.tv_autoignition_insurance = (TextView) convertView.findViewById(R.id.tv_autoignition_insurance);
            childViewHolder.tv_responsibility_insurance = (TextView) convertView.findViewById(R.id.tv_responsibility_insurance);
            childViewHolder.tv_wading_insurance = (TextView) convertView.findViewById(R.id.tv_wading_insurance);
            childViewHolder.tv_scratch_insurance = (TextView) convertView.findViewById(R.id.tv_scratch_insurance);
            childViewHolder.tv_glass_fracture = (TextView) convertView.findViewById(R.id.tv_glass_fracture);
            childViewHolder.tv_not_calculation = (TextView) convertView.findViewById(R.id.tv_not_calculation);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        return convertView;
    }

    static class ChildViewHolder {
        private TextView mTvInsureData;
        private TextView tv_car_cost;
        private TextView tv_car_loss_insurance;
        private TextView tv_insurance_money;
        private TextView tv_personnel_insurance;
        private TextView tv_compulsory_insurance;
        private TextView tv_third_party;
        private TextView tv_theft_insurance;
        private TextView tv_autoignition_insurance;
        private TextView tv_responsibility_insurance;
        private TextView tv_wading_insurance;
        private TextView tv_scratch_insurance;
        private TextView tv_glass_fracture;
        private TextView tv_not_calculation;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
